package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.IView.IPauseOrResetEngineeringOrderView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PhotoBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.engineering.customview.PhotoView;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PauseOrResetEngineeringOrderPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;
import com.cloudsigns.factorymanager2.project.activity.AddProductInfomationActivity1;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;


public class ViewEngineeringInstallOrderDetailActivity1 extends Activity implements View.OnClickListener, IEngineeringInstallOrderDetailView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView edt_project_name, edt_engineering_name, edt_engineering_address, edt_install_cycle;
    private ListView lv;
    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private final String TAG = ViewEngineeringInstallOrderDetailActivity.class.getSimpleName();
    EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    OrderParentBean orderParentBean;
    private EngineeringInstallOrderDetailPresenter engineeringInstallOrderDetailPresenter;
    private GridView gridView;
    private GridAdapter gridAdapter;
    private List<String> serverUrlList = new ArrayList<>();
    private String orderId = "";
    private CommonEnum.EngineeringORderCategory type;

    private ImageView img_install_cycle, img_edit_engineering_information;
    private LinearLayout ll_save;
    private Button btn_save;

    private GeoCoder mSearch = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_view_engineering_install_order_detail1);

        engineeringInstallOrderDetailPresenter = new EngineeringInstallOrderDetailPresenter(this);

        init();

        gridView = findViewById(R.id.gridView);
        gridView.setNumColumns(3);
        gridAdapter = new GridAdapter(this);
        gridAdapter.setData(serverUrlList);
        gridView.setAdapter(gridAdapter);

        Intent mIntent = getIntent();
        if (null != mIntent) {
            String tag = mIntent.getStringExtra(ConstantUtil.KEY_1);

            if (!TextUtils.isEmpty(tag)) {
                if (tag.equals("SendOrderForSuperActivity") || tag.equals("DispatchEngineeringInstallationOrderActivity")) {
                    engineeringInstallationOrderBean = (EngineeringInstallationOrderBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

                    // ll_btn.setVisibility(View.GONE);
                } else if (tag.equals("EngineeringInstallStatusTableForManagerActivity")) {
                    EngineeringInstallStatuBean engineeringInstallStatuBean = (EngineeringInstallStatuBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

                    if (null != engineeringInstallStatuBean)
                        orderId = engineeringInstallStatuBean.getOrderId();

                    type = CommonEnum.EngineeringORderCategory.INSTALL;

                    engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();

                } else {
                    orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

                    if (null != orderParentBean) {
                        orderId = orderParentBean.getId();
                        type = orderParentBean.getCategory();
                    }

                    CommonEnum.FeedBackState feedBackState = orderParentBean.getFeedBackState();
                    if (CommonEnum.FeedBackState.PAUSE == feedBackState || CommonEnum.FeedBackState.COMPLETE == feedBackState) {
                        //  ll_btn.setVisibility(View.GONE);
                    } else {
                        // ll_btn.setVisibility(View.VISIBLE);

                    }
                    engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();
                }
            }
            initData();
        }
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt_project_name = findViewById(R.id.edt_project_name);
        edt_engineering_name = findViewById(R.id.edt_engineering_name);
        edt_engineering_address = findViewById(R.id.edt_engineering_address);
        edt_install_cycle = findViewById(R.id.edt_install_cycle);
        lv = findViewById(R.id.lv);
        img_install_cycle = findViewById(R.id.img_install_cycle);
        img_edit_engineering_information = findViewById(R.id.img_edit_engineering_information);
        ll_save = findViewById(R.id.ll_save);
        btn_save = findViewById(R.id.btn_save);

        tv_title.setText(getResources().getText(R.string.work_order_information));

        img_edit_engineering_information.setBackgroundResource(R.mipmap.arrow_down_icon);

        rl_back.setOnClickListener(this);
        img_install_cycle.setOnClickListener(this);
        img_edit_engineering_information.setOnClickListener(this);
        btn_save.setOnClickListener(this);

        adapter = new ProductInformationAdapter(this, TAG);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);
    }

    private void initData() {
        if (null != engineeringInstallationOrderBean) {
            edt_project_name.setText(engineeringInstallationOrderBean.getProjectName());
            edt_engineering_name.setText(engineeringInstallationOrderBean.getEngineeringName());
            edt_engineering_address.setText(engineeringInstallationOrderBean.getEngineeringAddress());
//            String beginDate = DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getBeginDate());
            String finishDate = DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getCompleteDate());
//            if (null == beginDate || TextUtils.isEmpty(beginDate))
//                beginDate = "~";

            if (null == finishDate || TextUtils.isEmpty(finishDate))
                finishDate = "~";

            edt_install_cycle.setText(finishDate);

            List<String> list = engineeringInstallationOrderBean.getPicUrl();
            if (null != list) {
                serverUrlList.clear();
                serverUrlList.addAll(list);
                gridAdapter.setData(serverUrlList);
                gridAdapter.notifyDataSetChanged();
            }

            if (null != engineeringInstallationOrderBean.getProductList()) {
                productBeanList.addAll(engineeringInstallationOrderBean.getProductList());
                adapter.setData(productBeanList);
                adapter.notifyDataSetChanged();
            }

        } else if (null != orderParentBean) {
            edt_project_name.setText(orderParentBean.getProjectName());
            edt_engineering_name.setText(orderParentBean.getEngineeringName());
        }
    }


    // 产品清单 收放
    private void showEngineeringInfoSelect() {
        if (lv.getVisibility() == View.VISIBLE) {
            lv.setVisibility(View.GONE);
            img_edit_engineering_information.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv.setVisibility(View.VISIBLE);
            img_edit_engineering_information.setBackgroundResource(R.mipmap.arrow_top);
        }
    }


    // 完成日期
    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_install_cycle.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }

    // 保存
    private void save() {
        mSearch.geocode(new GeoCodeOption()
//                .city(edt_engineering_city.getText().toString().trim())
                .city("广东")
                .address(edt_engineering_address.getText().toString().trim()));

    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 完成日期
            case R.id.img_install_cycle:
                showDateDialog();
                break;
            // 产品信息
            case R.id.img_edit_engineering_information:
                showEngineeringInfoSelect();
//                mIntent = new Intent(this, AddProductInfomationActivity1.class);
//                mIntent.putExtra("type", "engineeringInstall");
//                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_INFOMATION);
                break;
            // 保存
            case R.id.btn_save:
                save();
                break;
        }
    }


    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public long getFeedbackDate() {
        return 0;
    }

    @Override
    public String getOrderId() {

        return orderId;
    }

    @Override
    public void getEngineeringInstallOrderDetailSuccessed(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;
        initData();

    }

    @Override
    public void getEngineeringInstallOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}
